﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;

namespace DB
{

    public class DbCinemaHall : DB.IDbCinemaHall
    {
        private DbConnection con;
        private SqlCommand dbCmd;

        public DbCinemaHall()
        {
            con = DbConnection.GetInstance();
            dbCmd = null;
        }

        public CinemaHall FindCinemaHallNew(string cName, bool retriveAssociation) // Finder et objekt med en List af Seats. "Single result"
        {
            SqlDataReader dbReader = null;
            CinemaHall cinHallObj = null;

            string select = "SELECT * FROM CinemaHall_xp WHERE cName = @cName";
            Console.WriteLine(select);

            try
            {
                dbCmd = con.GetDbCommand(select);
                dbCmd.Parameters.AddWithValue("@cName", cName);
                dbReader = dbCmd.ExecuteReader();
                if (dbReader.Read())
                {
                    cinHallObj = BuildCinemaHallNew(dbReader);
                }
                else
                {
                    cinHallObj = null;
                }

                dbReader.Close();

                if (retriveAssociation && cinHallObj != null)
                {
                    IDbSeat dbSeat = new DbSeat();
                    cinHallObj.Seats = dbSeat.FindSeatsNew(cName, false); // Seat får ingen assocering til et cinemaHallObjekt.
                }
            }

            catch (SqlException e)
            {
                Console.WriteLine("sql Error in DbCinemaHall");
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in DbCinemaHall");
                Console.WriteLine(e.Message);
            }


            con.CloseConnection();

            return cinHallObj;
        }
        public List<CinemaHall> FindAllCinemaHalls()
        {
            SqlDataReader dbReader = null;
            CinemaHall cinHallObj = null;
            List<CinemaHall> list = new List<CinemaHall>();
            string select = "SELECT * FROM CinemaHall_xp";
            Console.WriteLine(select);

            try
            {
                dbCmd = con.GetDbCommand(select);
                dbReader = dbCmd.ExecuteReader();
                while (dbReader.Read())
                {
                    cinHallObj = BuildCinemaHallNew(dbReader);
                    list.Add(cinHallObj);
                }

                dbReader.Close();

            }

            catch (SqlException e)
            {
                Console.WriteLine("sql Error in DbCinemaHall");
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in DbCinemaHall");
                Console.WriteLine(e.Message);
            }

            con.CloseConnection();
            return list;
        }

        private CinemaHall BuildCinemaHallNew(SqlDataReader cinemaReader)
        {
            CinemaHall cinemaHallObj = new CinemaHall();
            List<Seat> seats = new List<Seat>();

            try
            {
                cinemaHallObj.Name = cinemaReader["cName"].ToString();
                cinemaHallObj.Seats = seats;
            }

            catch (Exception e)
            {
                Console.WriteLine("Error in building cinemahallObj");
                Console.WriteLine(e.Message);
            }

            return cinemaHallObj;
        }
    }
}
